package yingyong;

import gongju.DBUtil;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.sql.SQLException;

@WebServlet(name = "add", value = "/add")
public class add extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        String sql = "insert into student values(1,'张三','男'),(2,'李四','女'),(3,'王五','男');";
        response.setCharacterEncoding("utf-8");
        try {
            int i = DBUtil.update(sql);
            if (i>0){
                response.getWriter().write("<script>alert('添加成功');location.href='/index.jsp';</script>");
            }else {
                response.getWriter().write("添加失败");
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }
}
